package me.youline.dataServer.service;

import java.util.Date;

import javax.transaction.Transactional;

import me.youline.dataServer.dao.UserDeviceTokenDAO;
import me.youline.dataServer.entity.UserDeviceToken;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserDeviceTokenService {

	@Autowired
	private UserDeviceTokenDAO userDeviceTokenDAO;
	
	
	@Transactional
	public void addUserDeviceToken(Long userId, String deviceToken){
		UserDeviceToken token = new UserDeviceToken();
		token.setUserId(userId);
		token.setDeviceToken(deviceToken);
		token.setUpdateTime(new Date());
		userDeviceTokenDAO.addUserDeviceToken(token);
	}
	
	@Transactional
	public void updateUserDeviceToken(Long userId,String newDeviceToken){
		int n =userDeviceTokenDAO.updateUserDeviceToken(userId, newDeviceToken);
		if( n <= 0 ){
			this.addUserDeviceToken(userId, newDeviceToken);
			return;
		}
	}
	
	@Transactional
	public void evictDeviceToken(Long userId){
		userDeviceTokenDAO.evictDeviceToken(userId);
	}
	
	@Transactional
	public UserDeviceToken getUserDeviceToken(Long userId){
		return userDeviceTokenDAO.getUserDeviceToken(userId);
	}
}
